package com.bestv.bsr.core.sentinel;

import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.bestv.bsr.core.properties.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * sentinel 加载rules
 *
 * @author taojiacheng
 */
@Configuration
@Slf4j
@RequiredArgsConstructor
public class SentinelConfiguration {

    private final AHRecommendProperties ahRecommendProperties;
    private final ZTRecommendProperties ztRecommendProperties;
    private final JTRecommendProperties jtRecommendProperties;
    private final HBRecommendProperties hbRecommendProperties;

    @PostConstruct
    public void updateSentinelInfo() {
        List<DegradeRule> degradeRules = getDegradeRules();
        log.info(">>>>>>>>>> load sentinel rules : {}", degradeRules);
        DegradeRuleManager.loadRules(getDegradeRules());
    }

    private List<DegradeRule> getDegradeRules() {
        List<DegradeRule> rules = new ArrayList<>();
        rules.add(getAhydDegradeRule());
        rules.add(getHbydDegradeRule());
        rules.add(getBestvDegradeRule());
        rules.add(getJTDegradeRule());
        return rules;

    }

    /**
     * 九天 sentinel 配置
     */
    private DegradeRule getJTDegradeRule() {
        if (null == jtRecommendProperties.getSentinel()) {
            return new DegradeRule();
        }
        SentinelProperties sentinel = jtRecommendProperties.getSentinel();
        return new DegradeRule("JT")
                .setGrade(sentinel.getGrade())
                .setCount(sentinel.getCount())
                .setTimeWindow(sentinel.getTimeWindow())
                .setSlowRatioThreshold(sentinel.getSlowRatioThreshold())
                .setMinRequestAmount(sentinel.getMinRequestAmount())
                .setStatIntervalMs(sentinel.getStatIntervalMs());
    }

    /**
     * 安徽移动 sentinel 配置
     */
    private DegradeRule getAhydDegradeRule() {
        if (null == ahRecommendProperties.getSentinel()) {
            return new DegradeRule();
        }
        SentinelProperties sentinel = ahRecommendProperties.getSentinel();
        return new DegradeRule("AH")
                .setCount(sentinel.getCount())
                .setGrade(sentinel.getGrade())
                .setTimeWindow(sentinel.getTimeWindow())
                .setMinRequestAmount(sentinel.getMinRequestAmount())
                .setStatIntervalMs(sentinel.getStatIntervalMs());
    }

    /**
     * 湖北移动 sentinel 配置
     */
    private DegradeRule getHbydDegradeRule() {
        if (null == hbRecommendProperties.getSentinel()) {
            return new DegradeRule();
        }
        SentinelProperties sentinel = hbRecommendProperties.getSentinel();
        return new DegradeRule("HB")
                .setCount(sentinel.getCount())
                .setGrade(sentinel.getGrade())
                .setTimeWindow(sentinel.getTimeWindow())
                .setMinRequestAmount(sentinel.getMinRequestAmount())
                .setStatIntervalMs(sentinel.getStatIntervalMs())
                .setSlowRatioThreshold(1);
    }

    private DegradeRule getBestvDegradeRule() {
        if (null == ztRecommendProperties.getSentinel()) {
            return new DegradeRule();
        }
        SentinelProperties sentinel = ztRecommendProperties.getSentinel();
        return new DegradeRule("ZT")
                .setCount(sentinel.getCount())
                .setGrade(sentinel.getGrade())
                .setTimeWindow(sentinel.getTimeWindow())
                .setMinRequestAmount(sentinel.getMinRequestAmount())
                .setStatIntervalMs(sentinel.getStatIntervalMs());
    }
}
